﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using Session;


public partial class CoreData : System.Web.UI.Page
{
    Service objService = new Service();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];
            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }

            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillConverConditionDetails(Convert.ToInt32(id));
                txtConvId.Text = string.Empty;
                trdgConvConditions.Visible = false;
                trConConditions.Visible = false;
            }

            bindDataGrid();
        }
    }
    private void ClearTextBoxes()
    {
        txtBedMedium.Text = string.Empty;
        txtConvId.Text = string.Empty;
        txtConvReg.Text = string.Empty;
        txtFeedRateMaximum.Text = string.Empty;
        txtFeedRateMinimum.Text = string.Empty;
        txtFeedSystem.Text = string.Empty;
        txtGasBiomass.Text = string.Empty;
        txtGasificationMedium.Text = string.Empty;
        txtHeatRateMaximum.Text = string.Empty;
        txtHeatRateMinimum.Text = string.Empty;
        txtPressure.Text = string.Empty;
        txtReactorConfig.Text = string.Empty;
        txtResidenceTimeMaximum.Text = string.Empty;
        txtResidenceTimeMinimum.Text = string.Empty;
        txtSweptGasVelocity.Text = string.Empty;
        txtTemeperature.Text = string.Empty;
    }

    private void bindDataGrid()
    {
        string ExpId = (string)Session["ExperimentId"];
        if (ExpId == null)
        {
            lblMessage.Text = "You haven't entered any data for the Conversion conditions to this experiment";
            trConConditions.Visible = true;
            trdgConvConditions.Visible = false;
        }
        else
        {
            Int32 ConversionId = objService.GetExperimentFKValues("convcond_fk", ExpId);
            if (ConversionId.ToString() == null)
            {
                lblMessage.Text = "You haven't entered any data for the Conversion conditions to this experiment";
                trConConditions.Visible = true;
                trdgConvConditions.Visible = false;
            }
            else
            {
                if (objService.GetConversionCondition(ConversionId.ToString()).Tables[0].Rows.Count > 0)
                {
                    dgConvConditions.DataSource = objService.GetConversionCondition(ConversionId.ToString());
                    dgConvConditions.DataBind();
                    trConConditions.Visible = false ;
                    trdgConvConditions.Visible = true ;
                }
                else
                {
                    trConConditions.Visible = true;
                    trdgConvConditions.Visible = false;
                }
            }
        }
    }

    protected void dgExperiment_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
   
    protected void dgConvConditions_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void dgConvConditions_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgConvConditions.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillConverConditionDetails(id);
        trConConditions.Visible = true;
        trdgConvConditions.Visible = false;
        btnPrevious.Visible = false;

    }
    protected void dgConvConditions_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgConvConditions.PageIndex = e.NewPageIndex;
        bindDataGrid();
    }

    private void clearTextboxes()
    {
        txtConvId.Text = string.Empty;
        txtConvReg.Text = string.Empty;
        txtReactorConfig.Text = string.Empty;
        txtBedMedium.Text = string.Empty;
        txtFeedSystem.Text = string.Empty;

        txtGasificationMedium.Text = string.Empty;
        txtGasBiomass.Text = string.Empty;
        txtFeedRateMinimum.Text = string.Empty;
        txtFeedRateMaximum.Text = string.Empty;


        txtTemeperature.Text = string.Empty;
        txtPressure.Text = string.Empty;
        txtHeatRateMinimum.Text = string.Empty;
        txtHeatRateMaximum.Text = string.Empty;

        txtResidenceTimeMinimum.Text = string.Empty;
        txtResidenceTimeMaximum.Text = string.Empty;
        txtSweptGasVelocity.Text = string.Empty;
        
    }

    private void fillConverConditionDetails(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("convconditions", id);
        txtConvId.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txtConvReg.Text = ds.Tables[0].Rows[0]["conv_Regime"].ToString();
        txtReactorConfig.Text = ds.Tables[0].Rows[0]["reactorConfig"].ToString();
        txtBedMedium.Text = ds.Tables[0].Rows[0]["bedMedium"].ToString();
        txtFeedSystem.Text = ds.Tables[0].Rows[0]["feedSystem"].ToString();

        txtGasificationMedium .Text = ds.Tables[0].Rows[0]["gasificationMed"].ToString();
        txtGasBiomass.Text = ds.Tables[0].Rows[0]["gasBiomassRatio"].ToString();
        txtFeedRateMinimum .Text = ds.Tables[0].Rows[0]["feedRate_min"].ToString();
        txtFeedRateMaximum.Text = ds.Tables[0].Rows[0]["feedRate_max"].ToString();

        txtTemeperature.Text = ds.Tables[0].Rows[0]["temp"].ToString();
        txtPressure.Text = ds.Tables[0].Rows[0]["pressure"].ToString();
        txtHeatRateMinimum.Text = ds.Tables[0].Rows[0]["heatRate_min"].ToString();
        txtHeatRateMaximum .Text = ds.Tables[0].Rows[0]["heatRate_max"].ToString();

        txtResidenceTimeMinimum.Text = ds.Tables[0].Rows[0]["resTime_min"].ToString();
        txtResidenceTimeMaximum.Text = ds.Tables[0].Rows[0]["resTime_max"].ToString();
        txtSweptGasVelocity.Text = ds.Tables[0].Rows[0]["sweptGasVelocity"].ToString();
        
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string RefeId = (string)Session["ReferenceId"];

        if (txtConvId.Text != string.Empty)
        {
            if (txtConvReg.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "conv_Regime", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "conv_Regime", txtConvReg.Text, txtConvId.Text);
            }

            if (txtReactorConfig.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "reactorConfig", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "reactorConfig", txtReactorConfig.Text, txtConvId.Text);
            }

            if (txtBedMedium.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "bedMedium", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "bedMedium", txtBedMedium.Text, txtConvId.Text);
            }

            if (txtFeedSystem.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "feedSystem", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "feedSystem", txtFeedSystem.Text, txtConvId.Text);
            }
            if (txtGasificationMedium.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "gasificationMed", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "gasificationMed", txtGasificationMedium.Text, txtConvId.Text);
            }

            if (txtGasBiomass.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "gasBiomassRatio", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "gasBiomassRatio", txtGasBiomass.Text, txtConvId.Text);
            }

            if (txtFeedRateMinimum.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "feedRate_min", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "feedRate_min", txtFeedRateMinimum.Text, txtConvId.Text);
            }
            if (txtFeedRateMaximum.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "feedRate_max", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "feedRate_max", txtFeedRateMaximum.Text, txtConvId.Text);
            }
            if (txtTemeperature.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "temp", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "temp", txtTemeperature.Text, txtConvId.Text);
            }

            if (txtPressure.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "pressure", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "pressure", txtPressure.Text, txtConvId.Text);
            }

            if (txtHeatRateMinimum.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "heatRate_min", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "heatRate_min", txtHeatRateMinimum.Text, txtConvId.Text);
            }

            if (txtHeatRateMaximum.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "heatRate_max", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "heatRate_max", txtHeatRateMaximum.Text, txtConvId.Text);
            }

            if (txtResidenceTimeMinimum.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "resTime_min", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "resTime_min", txtResidenceTimeMinimum.Text, txtConvId.Text);
            }
            if (txtResidenceTimeMaximum.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "resTime_max", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "resTime_max", txtResidenceTimeMaximum.Text, txtConvId.Text);
            }

            if (txtSweptGasVelocity.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "sweptGasVelocity", "null", txtConvId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("convconditions", "sweptGasVelocity", txtSweptGasVelocity.Text, txtConvId.Text);
            }

            bindDataGrid();

            ClearTextBoxes();
        }
        else
        {
            lblMessage.Text = objService.SaveConvConditions(txtConvReg.Text, txtReactorConfig.Text, txtBedMedium.Text, txtFeedSystem.Text, txtGasificationMedium.Text, txtGasBiomass.Text, txtFeedRateMinimum.Text, txtFeedRateMaximum.Text, txtTemeperature.Text, txtPressure.Text, txtHeatRateMinimum.Text, txtHeatRateMaximum.Text, txtResidenceTimeMinimum.Text, txtResidenceTimeMaximum.Text, txtSweptGasVelocity.Text);

            string ConCodId = objService.GetLastInsertedId("ref_fk", RefeId, "convconditions");
              
            objService.UpdateTable("convconditions", "ref_fk", RefeId, ConCodId);

           ClearTextBoxes();
                        
            if ((string)Session["ExperimentId"] != null)
            {
                string ExpId = (string)Session["ExperimentId"];
                if(objService.UpdateExperiment("convcond_fk", ConCodId, ExpId)!="Experiment Successfully Updated")
                {
                    objService.DeleteRecord("convconditions", Convert.ToInt32(ConCodId));
                    lblMessage.Text = "Admin - You can't enter the multiple Conversion conditions record for one experiment";
                    bindDataGrid();
                }
                else
                {
                lblMessage.Text = "Conversrion Conditions Saved and " + objService.UpdateExperiment("convcond_fk", ConCodId, ExpId);
                bindDataGrid();
                }
            }
            else
            {

                lblMessage.Text = "Conversrion Conditions Saved and " + objService.SaveExperiment(null, null, null, ConCodId, null, null, null, null, null, null, null, RefeId,null);
                string ExperimentId = objService.GetExperimentId("convcond_fk", ConCodId);
                Session["ExperimentId"] = ExperimentId;
                bindDataGrid();
            }
        }
       
    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreConvConditions.aspx");
    }
}
